|
|
|
|
|
|
|
In Chapter 14, "Creating ActiveX Controls," you learned the basics of creating an ActiveX control project in Visual Basic. You also used two add-in wizards to aid in control development. This chapter continues the discussion of ActiveX controls, with some additional topics that will help you build better controls. The following sections cover these topics: |
|
|
|
|
|
|
|
|
Using objects that enhance control integration with their containers |
|
|
|
|
|
|
|
|
Creating Property Pages for added flexibility without using a wizard |
|
|
|
|
|
|
|
|
Handling errors in your ActiveX controls |
|
|
|
|
|
|
|
|
You'll also build a slightly more involved ActiveX control, the Calculator control. |
|
|
|
|
|
|
|
|
Using the Ambient Object to Maintain Uniformity |
|
|
|
|
|
|
|
|
With a Standard EXE project, you have a great amount of control over what the user sees, such as the color and layout of the forms. However, ActiveX controls can be placed in a number of containers, determined by the user. In order for the control to behave in a consistent and desired manner in each type of container, it is necessary for the control to have access to information about its container. Fortunately, programmers can write code in a UserControl object that uses the Ambient object to determine information about its surroundings. |
|
|
|
|
|
|
|
|
Setting Up an Ambient Object Example |
|
|
|
|
|
|
|
|
To demonstrate a use of the Ambient object, start a new Standard EXE project, and then add an ActiveX Control project to create a project group. Resize the UserControl object so that it is fairly small (about 1605×1050 twips), and close the UserControl window. Next, set the BackColor property of Form1 to purple, and draw an instance of the user control on the form. The results should look similar to Figure 15.1. |
|
|
|
|
|
|
|
|
Figure 15.1
User controls will not automatically
respond to color changes in the container. |
|
|
|
|
|
|
|
|
Notice that the control's background color does not match that of the form. Most likely, the control would work properly, but the color difference would be a distraction to the users. For the control to match the background automatically, code within the UserControl object needs a way to retrieve the BackColor property of its container. Fortunately, the Ambient object makes this and other information available. |
|
|
|
|
|